1 module hip.systems.timer_manager;
2 import hip.util.reflection;
3 import hip.console.log;
4 public import hip.timer;
5 
6 class HipTimerManager
7 {
8     @disable this();
9     private __gshared IHipTimer[] timers;
10     private __gshared IHipTimer[] renderTimers;
11     private __gshared bool _isPaused = false;
12     private __gshared float deltaTime = 0;
13     private __gshared float accelerationFactor = 1.0f;
14 
15 
16     @ExportD static IHipTimer addTimer(IHipTimer timer)
17     {
18         timers~= timer.play();
19         return timer;
20     }
21 
22     @ExportD static IHipTimer addRenderTimer(IHipTimer timer)
23     {
24         renderTimers~= timer.play();
25         return timer;
26     }
27 
28     @ExportD static bool isPaused(){return _isPaused;}
29     @ExportD static void setPaused(bool shouldPause)
30     {
31         _isPaused = shouldPause;
32     }
33 
34     @ExportD static void setAccelerationFactor(float factor){accelerationFactor = factor;}
35     @ExportD static float getAccelerationFactor(){return accelerationFactor;}
36 
37     /**
38     *   This functions is used in development mode (should clear all the schedule after a hotreload)
39     */
40     static void clearSchedule()
41     {
42         renderTimers.length = 0;
43         timers.length = 0;
44     }
45 
46     /**
47     *   Update saves the delta time to be used on the render
48     */
49     static void update(float delta)
50     {
51         if(_isPaused)
52             return;
53         delta*= accelerationFactor;
54         this.deltaTime = delta;
55         int count = 0;
56         foreach(timer; timers)
57         {
58             count+= cast(int)timer.tick(delta);
59         }
60         if(count && count == timers.length)
61         {
62             timers.length = 0;
63         }
64     }
65 
66     /**
67     *   Rendered on top of everything.
68     *   Use it as a debug renderer
69     */
70     static void render()
71     {
72         int count = 0;
73         foreach(renderTimer; renderTimers)
74         {
75             count+= cast(int)renderTimer.tick(this.deltaTime);
76         }
77         if(count && count == renderTimers.length)
78             renderTimers.length = 0;
79     }
80 }